<template>
	<view class="container" style="min-height: 100vh;display: flex;flex-direction: column;">
		<view class="types">
			<view class="type-item" :class="type === 1 ? 'active' : ''" @click="changeType(1)">
				<image :src="img_url + '/type_car.png'" mode="widthFix" style="width: 350rpx;height: 230rpx;"></image>
				<view class="type-name">
					陆运运输
				</view>
				<view class="type-icon" v-if="type === 1">
					<icon type="success_no_circle" size="26" color="#2979ff"/>
				</view>
			</view>
			<view class="type-item" :class="type === 2 ? 'active' : ''" @click="changeType(2)">
				<image :src="img_url + '/type_ship.png'" mode="widthFix" style="width: 350rpx;height: 230rpx;"></image>
				<view class="type-name">
					水运运输
				</view>
				<view class="type-icon" v-if="type === 2">
					<icon type="success_no_circle" size="26" color="#2979ff"/>
				</view>
			</view>
		</view>
		<view class="footer-box safe-bottom" style="background-color: #fff;">
			<button class="reset-button type-btn" @click="onSubmit">确认</button>
		</view>
	</view>
</template>

<script>
	import { img_url } from '@/config/api.js';
	export default {
		data() {
			return {
				img_url: img_url,
				type: 1,
				eventChannel: null,
				tel: '',
				code: '',
				openid: '',
				login: ''
			}
		},
		onLoad() {
			const eventChannel = this.getOpenerEventChannel();
			if (!eventChannel) {
				uni.navigateBack({
					delta: 1
				})
				return
			}
			this.eventChannel = eventChannel;
			let that = this;
 
			eventChannel.on('login', function(params) {
				console.log(params)
				let {tel, code, openid, login} = params;
				that.tel = tel;
				that.code = code;
				that.openid = openid;
				that.login = login
			})
		},
		methods: {
			changeType(e) {
				this.type = e
			},
			onSubmit() {
				let type = this.type
				switch (this.login){
					case 'wechat':
						if (type === 1) {
							this.wxRequest({
								url: `${this.API}/miniApp/auth/wxLogin`,
								method: 'POST',
								data: {
									mobile: this.tel,
									openid: this.openid
								},
								title: '登录中...',
								success: res => {
									if (res.data.code === 0) {
										uni.setStorageSync(this.DATA_KEY.userInfo, res.data.data.userModel);
										uni.setStorageSync(this.DATA_KEY.token, res.data.data.token);
										if(res.data.data.userModel.etpId != '' && res.data.data.userModel.etpName != ''){
											uni.setStorageSync(this.DATA_KEY.etpId, res.data.data.userModel.etpId);
											uni.setStorageSync(this.DATA_KEY.etpName, res.data.data.userModel.etpName);
										}
										uni.setStorageSync('loginType', 1)
										uni.switchTab({
											url:'/pages/index/taskhome'
										});
									} else {
										uni.showToast({
											title: res.data.msg,
											icon: 'none',
											success() {
												setTimeout(() => {
													uni.navigateBack({
														delta: 1
													})
												},1200)
											}
										})
									}
									
								}
							});
						} else if (type === 2) {
							this.wxRequest({
								url: `${this.API}/miniApp/auth/wxLoginCaptain`,
								method: 'POST',
								data: {
									mobile: this.tel,
									openid: this.openid
								},
								title: '登录中...',
								success: res => {
									if (res.data.code === 0) {
										uni.setStorageSync(this.DATA_KEY.userInfo, res.data.data.userModel);
										uni.setStorageSync(this.DATA_KEY.token, res.data.data.token);
										if(res.data.data.userModel.etpId != '' && res.data.data.userModel.etpName != ''){
											uni.setStorageSync(this.DATA_KEY.etpId, res.data.data.userModel.etpId);
											uni.setStorageSync(this.DATA_KEY.etpName, res.data.data.userModel.etpName);
										}
										uni.setStorageSync('loginType', 2)
										console.log('跳转')
										uni.reLaunch({
											url:'/pages/ship/index/task'
										})
									} else {
										uni.showToast({
											title: res.data.msg,
											icon: 'none',
											success() {
												setTimeout(() => {
													uni.navigateBack({
														delta: 1
													})
												},1200)
											}
										})
									}
									
								}
							});
						}
						break;
					case 'sms':
						if (type === 1) {
							this.wxRequest({
									url: `${this.API}/miniApp/auth/sms-login?tel=` + this.tel + '&code=' + this.code + '&openid=' + this.openid,
									method: 'POST',
									title: '登录中...',
									success: res => {
										if (res.data.code === 0) {
											uni.setStorageSync(this.DATA_KEY.userInfo, res.data.data.userModel);
											uni.setStorageSync(this.DATA_KEY.token, res.data.data.token);
											if (res.data.data.userModel.etpId != '' && res.data.data.userModel.etpName != '') {
												uni.setStorageSync(this.DATA_KEY.etpId, res.data.data.userModel.etpId);
												uni.setStorageSync(this.DATA_KEY.etpName, res.data.data.userModel.etpName);
											}
											uni.setStorageSync('loginType', 1)
											uni.switchTab({
												url:'/pages/index/taskhome'
											});
										} else {
											uni.showToast({
												title: res.data.msg,
												icon: 'none',
												success() {
													setTimeout(() => {
														uni.navigateBack({
															delta: 1
														})
													},1200)
												}
											})
										}
								}
							})
						} else if (type === 2) {
							this.wxRequest({
									url: `${this.API}/miniApp/auth/sms-login-captain?tel=` + this.tel + '&code=' + this.code + '&openid=' + this.openid,
									method: 'POST',
									title: '登录中...',
									success: res => {
										if (res.data.code === 0) {
											uni.setStorageSync(this.DATA_KEY.userInfo, res.data.data.userModel);
											uni.setStorageSync(this.DATA_KEY.token, res.data.data.token);
											if (res.data.data.userModel.etpId != '' && res.data.data.userModel.etpName != '') {
												uni.setStorageSync(this.DATA_KEY.etpId, res.data.data.userModel.etpId);
												uni.setStorageSync(this.DATA_KEY.etpName, res.data.data.userModel.etpName);
											}
											uni.setStorageSync('loginType', 2)
											uni.reLaunch({
												url:'/pages/ship/index/task'
											});
										} else {
											uni.showToast({
												title: res.data.msg,
												icon: 'none',
												success() {
													setTimeout(() => {
														uni.navigateBack({
															delta: 1
														})
													},1200)
												}
											})
										}
								}
							})
						}
						break;
					default:
						break;
				}
				
				
			}
		},
		onUnload() {
			this.eventChannel.off('login');
		},
	}
</script>

<style lang="scss">
$color-primary: #2979ff;
.reset-button {
  padding: 0;
  margin: 0;
  font-size: inherit;
  background-color: transparent;
  color: inherit;
  position: relative;
  border: 0rpx;
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  text-align: center;
  text-decoration: none;
  white-space: nowrap;
  vertical-align: baseline;
  transform: translate(0, 0);
}
.reset-button.button-hover {
  transform: translate(1px, 1px);
  background: none;
}

.reset-button::after {
  border: none;
}

.types {
	flex: 1;
	.type-item {
		width: 650rpx;
		height: 450rpx;
		background-color: #fff;
		margin: 80rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		position: relative;
		&.active {
			border: 6rpx solid $color-primary;
		}
		.type-name {
			color: #555;
			font-size: 36rpx;
			margin-top: 80rpx;
		}
		.type-icon {
			position: absolute;
			right: 20rpx;
			bottom: 20rpx;
			display: flex;
		}
	}
}
.type-btn {
	background-color: $color-primary;
	color: #fff;
	width: 650rpx;
	height: 90rpx;
	margin: 20rpx auto;
}
.type-btn.button-hover {
	transform: none;
  background: lighten($color-primary, 10%);
}
.safe-bottom {
  padding-bottom: 0;
  padding-bottom: calc(constant(safe-area-inset-bottom) / 5 * 3);
  padding-bottom: calc(env(safe-area-inset-bottom) / 5 * 3);
}

</style>
